package com.agency8x.web.util;

import java.util.ArrayList;
import java.util.List;

import com.agency8x.bean.ResultInfoBean;
import com.jfinal.core.Controller;

// 2017-5-16
public class ValidatorUtil {

	public static void errorRedirect(Controller controller, String[] names) {
		errorRedirect(controller, null, names);
	}

	public static void errorRedirect(Controller controller, String url, String[] names) {
		List<String> messages = new ArrayList<String>();
		int i = 0;
		String msg;
		for (String name : names) {
			msg = controller.getAttr(name);
			if (null != msg) {
				messages.add(msg);
				i++;
			}
		}
		if (i > 0) {
			ControllerUtil.renderDisptchJumpError(controller, messages.toArray(new String[i]), url); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}
	}

	public static void errorJson(Controller controller, String[] names) {
		List<String> messages = new ArrayList<String>();
		int i = 0;
		String msg;
		for (String name : names) {
			msg = controller.getAttr(name);
			if (null != msg) {
				messages.add(msg);
				i++;
			}
		}
		if (i > 0) {
			ResultInfoBean resultInfoBean=new ResultInfoBean();
			resultInfoBean.setResult(false);
			resultInfoBean.setMessage(String.join("<br />", messages));
			controller.renderJson(resultInfoBean);
		}
		
	}
}
